#
# Copyright (c) 1999 2000
# Information Promotion Agency of Japan (IPA),
# Fujitsu Limited, and 
# Matsuoka Laboratory, Tokyo Institute of Technology
# All rights reserved.
#
# Redistribution and non-commercial use in source and binary forms, 
# with or without modification, are permitted provided that the following 
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# 3. All advertising materials mentioning features or use of this 
#    software must display the following acknowledgement:
#
#    This product includes software developed by Information Promotion Agency
#    of Japan, Fujitsu Limited, and Matsuoka Laboratory, 
#    Tokyo Institute of Technology.
#
# 4. The names "Information Promotion Agency", "Fujitsu Limited",
#    "Matsuoka Laboratory", or "Tokyo Institute of Technology" should
#    not be used to endorse or promote products derived from this
#    software without specific prior written permission.
#
# 5. Any use of the source code or the binary in a commercial product, 
#    whether may it be the origial representation or in some modified form,
#    is not permitted without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Revision: 1.43 $
# $Date: 2000/03/14 09:05:17 $
# $Author: kouya $
#

srcdir = .

BUILD_DIR = @BUILD_DIR@
PACKAGE = @PACKAGE@
VERSION = @VERSION@

LIBRARY = OpenJIT
PLATFORM = @PLATFORM@
ARCH = @ARCH@

#
# Directories
#
JAVA_HOME = @JAVA_HOME@

INSTALL = ./install-sh -c
INSTALL_LIBDIR = @INSTALL_LIBDIR@
INSTALL_CLASSDIR = @INSTALL_CLASSDIR@

OBJS = $(OBJDIR)/api.o $(OBJDIR)/stubs.o $(OBJDIR)/jdklink.o $(OBJDIR)/rt-$(ARCH).o 

SOURCES = api.c stubs.c jdklink.c rt-Sparc.c rt-X86.c
HEADERS = OpenJIT.h

JAVA_SOURCES = \
	org/OpenJIT/BCinfo.java \
	org/OpenJIT/Compile.java \
	org/OpenJIT/CompilerError.java \
	org/OpenJIT/Constants.java \
	org/OpenJIT/ConvertRTL.java \
	org/OpenJIT/ExceptionHandler.java \
	org/OpenJIT/ILnode.java \
	org/OpenJIT/LinkedList.java \
	org/OpenJIT/OptimizeRTL.java \
	org/OpenJIT/OpenJITLoader.java \
	org/OpenJIT/ParseBytecode.java \
	org/OpenJIT/Sparc.java \
	org/OpenJIT/X86.java \
	org/OpenJIT/Debug.java

LIB_LOCATION = .

CLEANFILES = libOpenJIT*.so org/OpenJIT/*.class
CLEANDIRS = obj obj_g CClassHeaders
CONFIG_CLEAN_FILES = Makefile platform.h config.cache config.log config.status

CONFIG_FILES = Makefile.in acconfig.h config.guess config.sub \
	configure configure.in platform.h.in Makefile.in

CONFIG_HEADER = platform.h
DIST_COMMON =  README INSTALL Makefile.in acconfig.h platform.h.in \
	config.guess config.sub configure configure.in install-sh 
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(JAVA_SOURCES)

TAR = gtar
GZIP_ENV = --best

#
# Rules
#

.PHONY: all optimized debug clean

all: debug optimized

library: debug optimized

OpenJIT.jar:
	jar cvf OpenJIT.jar org/OpenJIT/*.class

CC = @CC@

LIBRARY_SUFFIX = so
DEBUG     = $(LIB_LOCATION)/lib$(LIBRARY)_g.$(LIBRARY_SUFFIX)
OPTIMIZED =  $(LIB_LOCATION)/lib$(LIBRARY).$(LIBRARY_SUFFIX)

OPT_SUFFIX      =
DBG_SUFFIX      = _g
SUFFIX          = $($(VARIANT)_SUFFIX)
OBJDIR          = obj$(SUFFIX)

CFLAGS_COMMON	= -g -Wall @CFLAGS@
CFLAGS_OPT	= -O2
CFLAGS		= $(CFLAGS_$(VARIANT)) $(CFLAGS_COMMON) $(OTHER_CFLAGS)
INCLUDES	= -I@JDK_INCLUDE@ -I@JDK_INCLUDE@/$(PLATFORM) -I./CClassHeaders
OTHER_CPPFLAGS	= $(INCLUDES)
DEFINES		= -DARCH=\"@ARCH@\" \
		  -DJDK_VERSION_STRING=\"@JDK_VERSION_STRING@\" \
		  @DEFINES@
CPPFLAGS_COMMON	= -DHAVE_ALIGNED_DOUBLES -DHAVE_ALIGNED_LONGLONGS
CPPFLAGS_DBG	= -DDEBUG -DJCOV
CPPFLAGS        = $(CPPFLAGS_$(VARIANT)) $(CPPFLAGS_COMMON) $(OTHER_CPPFLAGS) \
		  $(DEFINES) $(OPTIONS:%=-D%)

JAVAC		= @JAVAC@
JAVACFLAGS	= @JAVACFLAGS@ -O
JAVAH		= @JAVAH@ -d CClassHeaders
JAVAHFLAGS	= @JAVAHFLAGS@

org/OpenJIT/%.class: org/OpenJIT/%.java
	$(JAVAC) $(JAVACFLAGS) $<

$(OBJDIR)/%.o: %.c
	$(COMPILE.c) -o $@ $<

debug:	.checkinstall org/OpenJIT/$(ARCH).class
	$(MAKE) $(DEBUG) VARIANT=DBG

optimized:	.checkinstall org/OpenJIT/$(ARCH).class
	$(MAKE) $(OPTIMIZED) VARIANT=OPT

.checkinstall:
	@if test -d $(INSTALL_CLASSDIR)/org/OpenJIT ; then \
	  echo "OpenJIT has been already installed."; \
	  echo "Please do \"make uninstall\" first."; \
	  exit 1; \
	fi

.objdir:
	@if test ! -d $(OBJDIR) ; then \
	  echo mkdir -p $(OBJDIR); \
	  mkdir -p $(OBJDIR); \
	fi

.libdir:
	@if test ! -d CClassHeaders ; then \
	  echo mkdir -p CClassHeaders; \
	  mkdir -p CClassHeaders; \
	fi

$(DEBUG): .objdir .libdir $(OBJS)
	$(LD) -G -o $@ $(OBJS) $(LDFLAGS) $(LDLIBS)

$(OPTIMIZED): .objdir .libdir $(OBJS)
	$(LD) -G -o $@ $(OBJS) $(LDFLAGS) $(LDLIBS)

install: all
	@if [ -f $(INSTALL_LIBDIR)/libzip.so ]; then \
	  echo $(INSTALL) libOpenJIT_g.so $(INSTALL_LIBDIR)/libOpenJIT_g.so; \
	  $(INSTALL) libOpenJIT_g.so $(INSTALL_LIBDIR)/libOpenJIT_g.so; \
	  echo $(INSTALL) libOpenJIT.so $(INSTALL_LIBDIR)/libOpenJIT.so; \
	  $(INSTALL) libOpenJIT.so $(INSTALL_LIBDIR)/libOpenJIT.so; \
	else \
	  if [ -f $(INSTALL_LIBDIR)/green_threads/libzip.so ]; then \
	    echo $(INSTALL) libOpenJIT_g.so $(INSTALL_LIBDIR)/green_threads/libOpenJIT_g.so; \
	    $(INSTALL) libOpenJIT_g.so $(INSTALL_LIBDIR)/green_threads/libOpenJIT_g.so; \
	    echo $(INSTALL) libOpenJIT.so $(INSTALL_LIBDIR)/green_threads/libOpenJIT.so; \
	    $(INSTALL) libOpenJIT.so $(INSTALL_LIBDIR)/green_threads/libOpenJIT.so; \
	  fi; \
	  if [ -f $(INSTALL_LIBDIR)/native_threads/libzip.so ]; then \
	    echo $(INSTALL) libOpenJIT_g.so $(INSTALL_LIBDIR)/native_threads/libOpenJIT_g.so; \
	    $(INSTALL) libOpenJIT_g.so $(INSTALL_LIBDIR)/native_threads/libOpenJIT_g.so; \
	    echo $(INSTALL) libOpenJIT.so $(INSTALL_LIBDIR)/native_threads/libOpenJIT.so; \
	    $(INSTALL) libOpenJIT_g.so $(INSTALL_LIBDIR)/native_threads/libOpenJIT_g.so; \
	  fi; \
	fi
	@for file in org/OpenJIT/*.class; do \
	  echo $(INSTALL) $$file $(INSTALL_CLASSDIR)/$$file; \
	  $(INSTALL) $$file $(INSTALL_CLASSDIR)/$$file; \
	done

uninstall:
	rm -f $(INSTALL_LIBDIR)/libOpenJIT*.so $(INSTALL_LIBDIR)/*/libOpenJIT*.so
	rm -fr $(INSTALL_CLASSDIR)/org/OpenJIT

all debug optimized:

mostlyclean:

clean:
	rm -f $(CLEANFILES)
	rm -fr $(CLEANDIRS)

distclean: clean
	rm -f $(CONFIG_CLEAN_FILES)

maintainer-clean: distclean
	@echo "This command is intended for maintainers to use;"
	@echo "it deletes files that may require special tools to rebuild."
	rm -f $(MAINTAINERCLEANFILES)

distdir = $(PACKAGE)-$(VERSION)
top_distdir = $(distdir)

# This target untars the dist file and tries a VPATH configuration.  Then
# it guarantees that the distribution is self-contained by making another
# tarfile.
distcheck: dist
	-rm -rf $(distdir)
	GZIP=$(GZIP_ENV) $(TAR) zxf $(distdir).tar.gz
	mkdir $(distdir)/=build
	mkdir $(distdir)/=inst
	dc_install_base=`cd $(distdir)/=inst && pwd`; \
	cd $(distdir)/=build \
	  && ../configure --srcdir=.. --prefix=$$dc_install_base \
	  && $(MAKE) $(AM_MAKEFLAGS) \
	  && $(MAKE) $(AM_MAKEFLAGS) dist
	-rm -rf $(distdir)
	@banner="$(distdir).tar.gz is ready for distribution"; \
	dashes=`echo "$$banner" | sed s/./=/g`; \
	echo "$$dashes"; \
	echo "$$banner"; \
	echo "$$dashes"
dist: distdir
	-chmod -R a+r $(distdir)
	GZIP=$(GZIP_ENV) $(TAR) chozf $(distdir).tar.gz $(distdir)
	-rm -rf $(distdir)
dist-all: distdir
	-chmod -R a+r $(distdir)
	GZIP=$(GZIP_ENV) $(TAR) chozf $(distdir).tar.gz $(distdir)
	-rm -rf $(distdir)
distdir: $(DISTFILES)
	-rm -rf $(distdir)
	mkdir $(distdir)
	-chmod 777 $(distdir)
	mkdir $(distdir)/org
	-chmod 777 $(distdir)/org
	mkdir $(distdir)/org/OpenJIT
	-chmod 777 $(distdir)/org/OpenJIT
	here=`cd $(top_builddir) && pwd`; \
	top_distdir=`cd $(distdir) && pwd`; \
	distdir=`cd $(distdir) && pwd`; \
	for file in $(DISTFILES); do \
	  d=$(srcdir); \
	  if test -d $$d/$$file; then \
	    cp -pr $$/$$file $(distdir)/$$file; \
	  else \
	    test -f $(distdir)/$$file \
	    || ln $$d/$$file $(distdir)/$$file 2> /dev/null \
	    || cp -p $$d/$$file $(distdir)/$$file || :; \
	  fi; \
	done

.PHONY: distdir installcheck install uninstall all \
	clean mostlyclean distclean maintainer-clean

#
$(OBJDIR)/api.o: OpenJIT.h platform.h CClassHeaders/org_OpenJIT_Compile.h CClassHeaders/org_OpenJIT_ExceptionHandler.h

$(OBJDIR)/rt-$(ARCH).o: OpenJIT.h platform.h CClassHeaders/org_OpenJIT_Compile.h CClassHeaders/org_OpenJIT_$(ARCH).h

$(OBJDIR)/stubs.o: CClassHeaders/org_OpenJIT_Compile.c

CClassHeaders/org_OpenJIT_Compile.h:	org/OpenJIT/Compile.class
	$(JAVAH) $(JAVAHFLAGS) org.OpenJIT.Compile

CClassHeaders/org_OpenJIT_Compile.c:	org/OpenJIT/Compile.class
	$(JAVAH) $(JAVAHFLAGS) -stubs org.OpenJIT.Compile

CClassHeaders/org_OpenJIT_$(ARCH).h: org/OpenJIT/$(ARCH).class
	$(JAVAH) $(JAVAHFLAGS) org.OpenJIT.$(ARCH)

CClassHeaders/org_OpenJIT_ExceptionHandler.h: org/OpenJIT/ExceptionHandler.class
	$(JAVAH) $(JAVAHFLAGS) org.OpenJIT.ExceptionHandler
